package com.etc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;

import com.etc.dao.PersonDao;
import com.etc.entity.Person;
import com.etc.util.JdbcUtil;

public class PersonDaoImpl implements PersonDao {
	
	public void add(Person person) {

		try {
			Connection conn = JdbcUtil.getConn();
			String sql = "insert into user (id, account, pwd, name,balance,phone,address,state) values (null, ?, ? , ? , ? , ? , ? ,?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, person.getAccount());
			ps.setString(2, person.getPwd());
			ps.setString(3, person.getName());
			ps.setDouble(4, person.getBalance());
			ps.setString(5, person.getPhone());
			ps.setString(6, person.getAddress());
			ps.setInt(7, person.getState());
			ps.execute();
			JdbcUtil.closeAll(null, ps, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
// 到数据库查到用户名和密码是否存在
	public boolean check(String account, String pwd) {
			boolean flag=false;
			Connection conn = JdbcUtil.getConn();
			ResultSet rs=null;
			
			String sql="select account,pwd from user where account=? and pwd=? ";
			try {
				rs=JdbcUtil.doQuery(conn, sql, account,pwd);
				if(rs.next()){
				if(account.equals(rs.getString("account"))&&pwd.equals(rs.getString("pwd")))
					flag=true;
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
		return flag;
	}

	public Person findByAccount(String account) {
		Connection conn = JdbcUtil.getConn();
		Person person = null;
		String sql = "select * from user where account = ?";	
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, account);
			rs = ps.executeQuery();
			
			if(rs.next()){
				person = new Person();
				person.setId(rs.getInt("id"));
				person.setAccount(rs.getString("account"));
				person.setPwd(rs.getString("pwd"));
				person.setName(rs.getString("name"));
				person.setBalance(rs.getDouble("balance"));
				person.setPhone(rs.getString("phone"));
				person.setAddress(rs.getString("address"));
				person.setState(rs.getInt("state"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return person;
	}
	/**
	 * 修改用户余额
	 * @throws SQLException 
	 */
	public void modifyBalance(Connection conn,int id, double balance) throws SQLException {
		String sql="update user set balance=? where id=?";
		PreparedStatement ps = null;
		JdbcUtil.doUpate(conn, sql, balance,id);
		
	}/**
	
	** 通过id查看用户信息
	*
	*/
	public Person findPersonById(int id) {
		Person p=new Person();
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="select * from user where id = ?";
		conn=JdbcUtil.getConn();
		try {
			ps=conn.prepareStatement(sql);
			rs=JdbcUtil.doQuery(conn, sql, id);
			if(rs.next()){
				p.setId(rs.getInt("id"));
				p.setAccount(rs.getString("account"));
				p.setPwd(rs.getString("pwd"));
				p.setName(rs.getString("name"));
				p.setBalance(rs.getDouble("balance"));
			}
			JdbcUtil.closeAll(rs, ps, conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
	}

	

}
